<template>
  <div class="app-container">
    <el-table
      v-tableFit
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="模块号" align="center">
        <template slot-scope="scope">
          {{ scope.row.module }}
        </template>
      </el-table-column>
      <el-table-column label="IP" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运行时间" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.uptime | secondsFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="最近一次心跳" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.lastreport }}秒</span>
        </template>
      </el-table-column>
      <el-table-column label="负荷" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.load }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="是否启用" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.used | statusFilter">{{ scope.row.used }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.row.module)">
            OS详情
          </el-button>
          <el-button type="warning" size="mini" @click="handleResource(scope.row.module)">
            资源状态
          </el-button>
          <el-button type="success" size="mini" @click="handleChans(scope.row.module)">
            房间媒体
          </el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { getZwmpList } from '@/api/dashboard'
import { Message } from 'element-ui'
import { secondsFormat } from '@/utils/index'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        true: 'success',
        false: 'gray'
      }
      return statusMap[status]
    },
    secondsFilter(seconds) {
      return secondsFormat(seconds)
    }
  },
  data() {
    return {
      list: null,
      listLoading: true
    }
  },
  created() {
    this.getZwmpList()
  },
  methods: {
    getZwmpList() {
      this.listLoading = true
      getZwmpList().then(response => {
        this.list = response.data.zwmps
        this.listLoading = false
      }).catch(error => {
        Message.error(error)
      })

      const timer = setInterval(() => {
        getZwmpList().then(response => {
          this.list = response.data.zwmps
          this.listLoading = false
        }).catch(error => {
          Message.error(error)
        })
      }, 1600)

      // 通过$once来监听定时器，在beforeDestroy钩子可以被清除。
      this.$once('hook:beforeDestroy', () => {
        clearInterval(timer)
      })
    },
    handleDetail(index) {
      const to = 'detail/' + index
      this.$router.push({ path: to })
    },
    handleResource(index) {
      const to = 'zwmpresource/' + index
      this.$router.push({ path: to })
    },
    handleChans(index) {
      this.$router.push({
        name: 'zwmpchans',
        params: {
          module: index
        }
      })
    }
  }
}
</script>
